//
//  DKOrderListCell.m
//  DanKU
//
//  Created by EDY on 2023/10/30.
//

#import "DKOrderListCell.h"
#import "DKApplicationOrderDetailVc.h"
#import "DKPublicHttp.h"
#import "OrderTool.h"
#import "DKOrderRepayViewController.h"

@interface DKOrderListCell()

@property(nonatomic,strong)UIView *dk_OrderCellView;
@property(nonatomic,strong)UIImageView *dk_OrderIcon_BackImg;
@property(nonatomic,strong)UIImageView *dk_OrderIconImg;
@property(nonatomic,strong)UILabel *dk_OrderTitlelab;
@property(nonatomic,strong)UILabel *dk_OrderStatelab;
@property(nonatomic,strong)UIButton *dk_OrderInfoBtn;
@property(nonatomic,strong)UILabel *dk_OrderLine;
@property(nonatomic,strong)UILabel *dk_OrderCountlab;
@property(nonatomic,strong)UILabel *dk_OrderDatelab;
@property(nonatomic,strong)UILabel *dk_OrderCountStatelab;
@property(nonatomic,strong)UILabel *dk_OrderCountInfolab;
@property(nonatomic,strong)UILabel *dk_OrderRejectlab;

@end

@implementation DKOrderListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor =  DK_Hex(@"#FAFAFA");
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatUI];
    }
    return  self;
}

-(void)setDateSouce:(DKOrderRecordListModel *)dateSouce{
    _dateSouce = dateSouce;
    self.dk_OrderStatelab.textColor = DK_Hex(@"#333333");
    self.dk_OrderInfoBtn.hidden = NO;
    self.dk_OrderStatelab.hidden = NO;
    self.dk_OrderRejectlab.hidden = YES;
    self.dk_OrderTitlelab.text = dateSouce.productName;
    
    switch (dateSouce.status) {
        case 90:{
            self.dk_OrderStatelab.text = @"Under review";
        }
            break;
        case 100:{
            self.dk_OrderStatelab.text = @"Approved";
        }
            break;
        case 110:{
            self.dk_OrderInfoBtn.hidden = YES;
            self.dk_OrderStatelab.hidden = YES;
            self.dk_OrderRejectlab.hidden = NO;
            self.dk_OrderRejectlab.text = @"Rejected";
        }
            break;
            
        case 150:{
            self.dk_OrderStatelab.text = @"Issuing";
        }
            break;
            
        case 169:{
            self.dk_OrderStatelab.text = @"Issue failed";
            if (!self.dk_OrderStatelab) {
                self.dk_OrderStatelab.textColor = [UIColor redColor];
            }
            
        }
            break;
            
        case 170:{
            self.dk_OrderStatelab.text = @"Issued";
        }
            break;
            
        case 180:{
            self.dk_OrderStatelab.text = @"Overdue";
            if (self.dk_OrderStatelab) {
                self.dk_OrderStatelab.textColor = [UIColor redColor];
            }
        }
            break;
        case 120:{
            self.dk_OrderStatelab.text = @"Settled";
            self.dk_OrderInfoBtn.hidden = YES;
        }
            break;
        default:
            break;
    }
    
    DKBeforePaymentDetail *model = dateSouce.beforePaymentDetail;
    self.dk_OrderCountlab.text = [OrderTool formatAmountForIndia:model.loanAmount];
    NSString *formattedDateString = [OrderTool stringFromTimestampString:model.applyTime];
    NSString  *changeString = [OrderTool convertDate:formattedDateString fromFormat:@"yyyy-MM-dd HH:mm:ss" toFormat:@"dd-MM-yyyy"];
    self.dk_OrderDatelab.text = changeString;
}

-(void)creatUI{
    [self.contentView addSubview:self.dk_OrderCellView];
    [self.dk_OrderCellView addSubview:self.dk_OrderIcon_BackImg];
    [self.dk_OrderCellView addSubview:self.dk_OrderIconImg];
    [self.dk_OrderCellView addSubview:self.dk_OrderTitlelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderStatelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderInfoBtn];
    [self.dk_OrderCellView addSubview:self.dk_OrderLine];
    [self.dk_OrderCellView addSubview:self.dk_OrderCountlab];
    [self.dk_OrderCellView addSubview:self.dk_OrderDatelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderCountStatelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderCountInfolab];
    
    [self.dk_OrderCellView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
    
    [self.dk_OrderIcon_BackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderCellView).offset(15);
        make.width.height.mas_equalTo(38);
        make.top.equalTo(self.dk_OrderCellView).offset(7);
    }];
    
    [self.dk_OrderIconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(26);
        make.center.equalTo(self.dk_OrderIcon_BackImg);
    }];

    [self.dk_OrderTitlelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderIcon_BackImg.mas_right).offset(5);
        make.width.mas_equalTo(DK_SCREEN_W/3.4);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(self.dk_OrderIcon_BackImg);
    }];

    [self.dk_OrderStatelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dk_OrderInfoBtn.mas_left).offset(-10);
        make.width.height.equalTo(self.dk_OrderTitlelab);
        make.centerY.equalTo(self.dk_OrderIcon_BackImg);
    }];

    [self.dk_OrderInfoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dk_OrderCellView.mas_right).offset(-15);
        make.width.height.mas_equalTo(16);
        make.centerY.equalTo(self.dk_OrderIcon_BackImg);
    }];

    [self.dk_OrderLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderCellView).offset(15);
        make.right.equalTo(self.dk_OrderCellView).offset(-15);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.dk_OrderIcon_BackImg.mas_bottom).offset(5);
    }];

    [self.dk_OrderCountlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderIcon_BackImg);
        make.height.equalTo(self.dk_OrderTitlelab);
        make.width.mas_equalTo(DK_SCREEN_W/2.5);
        make.top.equalTo(self.dk_OrderLine.mas_bottom).offset(20);
    }];

    [self.dk_OrderDatelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dk_OrderCellView).offset(-15);
        make.width.height.top.equalTo(self.dk_OrderCountlab);
    }];

    [self.dk_OrderCountStatelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.left.equalTo(self.dk_OrderCountlab);
        make.top.equalTo(self.dk_OrderCountlab.mas_bottom).offset(10);
    }];

    [self.dk_OrderCountInfolab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.right.equalTo(self.dk_OrderDatelab);
        make.top.equalTo(self.dk_OrderCountStatelab);
    }];
    
    [self.dk_OrderCellView addSubview:self.dk_OrderRejectlab];
    [self.dk_OrderRejectlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dk_OrderCellView.mas_right).offset(-15);
        make.width.height.equalTo(self.dk_OrderTitlelab);
        make.centerY.equalTo(self.dk_OrderIcon_BackImg);
    }];
}

#pragma mark - 详情
-(void)toDetailAction{
    if (self.dateSouce.status == 180) {
        DKOrderRepayViewController *repay = [DKOrderRepayViewController new];
//        repay.orderNo = self.dateSouce.orderNo;
        repay.dateSouce = self.dateSouce;
        [[self viewController].navigationController pushViewController:repay animated:YES];
    }else{
        DKApplicationOrderDetailVc *vc = [DKApplicationOrderDetailVc new];
        vc.orderNo = self.dateSouce.orderNo;
        [[self viewController].navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - 懒加载
-(UIView*)dk_OrderCellView{
    if (!_dk_OrderCellView) {
        _dk_OrderCellView = [[UIView alloc]init];
        _dk_OrderCellView.backgroundColor = DK_Hex(@"#FFFFFF");
        _dk_OrderCellView.layer.cornerRadius = 10;
    }
    return _dk_OrderCellView;
}

- (UIImageView*)dk_OrderIcon_BackImg{
    if (!_dk_OrderIcon_BackImg) {
        _dk_OrderIcon_BackImg = [[UIImageView alloc] init];
        _dk_OrderIcon_BackImg.image =  DK_GetImage(@"dk_order_list_back_img");
    }
    return _dk_OrderIcon_BackImg;
}

- (UIImageView*)dk_OrderIconImg{
    if (!_dk_OrderIconImg) {
        _dk_OrderIconImg = [[UIImageView alloc] init];
        _dk_OrderIconImg.image =  DK_GetImage(@"dk_order_list_img");
    }
    return _dk_OrderIconImg;
}

- (UILabel *)dk_OrderTitlelab {
    if (!_dk_OrderTitlelab) {
        _dk_OrderTitlelab = [[UILabel alloc] init];
        _dk_OrderTitlelab.textColor = DK_Hex(@"#333333");
        _dk_OrderTitlelab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderTitlelab.text = @"Go 002";
    }
    return _dk_OrderTitlelab;
}


- (UILabel *)dk_OrderStatelab {
    if (!_dk_OrderStatelab) {
        _dk_OrderStatelab = [[UILabel alloc] init];
        _dk_OrderStatelab.textColor = DK_Hex(@"#333333");
        _dk_OrderStatelab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderStatelab.text = @"Under review";
        _dk_OrderStatelab.textAlignment = 2;
    }
    return _dk_OrderStatelab;
}

- (UILabel *)dk_OrderRejectlab {
    if (!_dk_OrderRejectlab) {
        _dk_OrderRejectlab = [[UILabel alloc] init];
        _dk_OrderRejectlab.textColor = DK_Hex(@"#333333");
        _dk_OrderRejectlab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderRejectlab.text = @"Under review";
        _dk_OrderRejectlab.textAlignment = 2;
        _dk_OrderRejectlab.hidden = YES;
    }
    return _dk_OrderRejectlab;
}



-(UIButton*)dk_OrderInfoBtn{
    if (!_dk_OrderInfoBtn) {
        _dk_OrderInfoBtn = [[UIButton alloc]init];
        [_dk_OrderInfoBtn setBackgroundImage:[UIImage imageNamed:@"icon_my_next_b"] forState:UIControlStateNormal];
        [_dk_OrderInfoBtn setBackgroundImage:[UIImage imageNamed:@"icon_my_next_b"] forState:UIControlStateSelected];
        [_dk_OrderInfoBtn addTarget:self action:@selector(toDetailAction) forControlEvents:UIControlEventTouchUpInside];
        _dk_OrderInfoBtn.hidden = NO;
    }
    return _dk_OrderInfoBtn;
}

- (UILabel *)dk_OrderLine {
    if (!_dk_OrderLine) {
        _dk_OrderLine = [[UILabel alloc] init];
        _dk_OrderLine.backgroundColor = DK_Hex(@"#E0E0E0");
    }
    return _dk_OrderLine;
}

- (UILabel *)dk_OrderCountlab {
    if (!_dk_OrderCountlab) {
        _dk_OrderCountlab = [[UILabel alloc] init];
        _dk_OrderCountlab.textColor = DK_Hex(@"#333333");
        _dk_OrderCountlab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderCountlab.text = @"₹3,000";
    }
    return _dk_OrderCountlab;
}

- (UILabel *)dk_OrderDatelab {
    if (!_dk_OrderDatelab) {
        _dk_OrderDatelab = [[UILabel alloc] init];
        _dk_OrderDatelab.textColor = DK_Hex(@"#333333");
        _dk_OrderDatelab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderDatelab.textAlignment = 2;
        _dk_OrderDatelab.text = @"18-07-2022";
    }
    return _dk_OrderDatelab;
}

- (UILabel *)dk_OrderCountStatelab {
    if (!_dk_OrderCountStatelab) {
        _dk_OrderCountStatelab = [[UILabel alloc] init];
        _dk_OrderCountStatelab.textColor = DK_Hex(@"#999999");
        _dk_OrderCountStatelab.font = [UIFont boldSystemFontOfSize:14];
        _dk_OrderCountStatelab.text = @"Loan amount";
    }
    return _dk_OrderCountStatelab;
}

- (UILabel *)dk_OrderCountInfolab {
    if (!_dk_OrderCountInfolab) {
        _dk_OrderCountInfolab = [[UILabel alloc] init];
        _dk_OrderCountInfolab.textColor = DK_Hex(@"#999999");
        _dk_OrderCountInfolab.font = [UIFont boldSystemFontOfSize:14];
        _dk_OrderCountInfolab.textAlignment = 2;
        _dk_OrderCountInfolab.text = @"Application date";
    }
    return _dk_OrderCountInfolab;
}

//获取View所在的Viewcontroller方法
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
